<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/9/009
 * Time: 9:06
 */
namespace redis;
class RedisPackage
{
    protected static $handler = null;
    private static $_instance = null;
    protected $options;

    private function __construct($options = [])
    {
        $this->options = [
            'host' =>config('redis')['host'] ? config('redis')['host'] : '127.0.0.1' ,
            'port' =>config('redis')['port'] ? config('redis')['port'] : 6379 ,
            'password' => config('redis')['password'] ? config('redis')['password'] : '' ,
            'select' => config('redis')['select'] ? config('redis')['select'] : 0 ,  //切换制定数据库
            'timeout' => config('redis')['timeout'] ? config('redis')['timeout'] : 0 ,//关闭时间 0:代表不关闭
            'persistent' =>  config('redis')['persistent'] ? config('redis')['persistent'] : false   //是否长连接
        ];
        if (!extension_loaded('redis')) {   //判断是否有扩展(如果你的apache没reids扩展就会抛出这个异常)
            throw new \BadFunctionCallException('not support: redis');
        }
        if (!empty($options)) {
            $this->options = array_merge($this->options, $options);
        }
        $func = $this->options['persistent'] ? 'pconnect' : 'connect'; //判断是否长连接
        self::$handler = new \Redis;
        self::$handler->$func($this->options['host'], $this->options['port'], $this->options['timeout']);
        if ('' != $this->options['password']) {
            self::$handler->auth($this->options['password']);
        }
        if (0 != $this->options['select']) {
            self::$handler->select($this->options['select']);
        }

    }


    /**
     * @return RedisPackage|null 对象
     */
    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {

            self::$_instance = new self();
        }
        return self::$_instance;
    }


    /**
     * 写入缓存
     * @param $key
     * @param $value
     * @param int $exprie
     * @return bool
     */
    public static function set($key, $value, $exprie = 0)
    {
        if(is_array($value)||is_object($value)){
            $value = json_encode($value);
        }
        if ($exprie == 0) {
            $set = self::$handler->set($key, $value);
        } else {
            $set = self::$handler->setex($key, $exprie, $value);
        }
        return $set;
    }

    /**
     * 写入缓存，如果KEY存在则不覆盖
     * @param $key
     * @param $value
     * @return bool
     */
    public function setnx($key,$value){
        return self::$handler->setnx($key, $value);
    }

    /** 判断key是否存在
     * @param $key
     * @return int
     */
    public static function exists($key){
        return self::$handler->exists($key);
    }

    /**
     * 读取缓存
     * @param string $key 为数组则返回所有指定键的值
     * @return mixed
     */
    public static function get($key)
    {
        $fun = is_array($key) ? 'mget' : 'get';
        return self::$handler->{$fun}($key);
    }


    /**
     * 删除指定键
     * @param $key
     * @return int
     */
    public static function del($key){
        return self::$handler->del($key);
    }


    /**
     * 设置哈希
     * @param $key
     * @param $hashKey
     * @param $value
     * @return bool|int
     */
    public static function hSet($key, $hashKey, $value ){
        return self::$handler->hSet($key, $hashKey, $value );
    }

    public static function hGet($key, $hashKey){
        return self::$handler->hGet($key, $hashKey);
    }

    /**
     * 删除哈希键
     * @param $key
     * @param $hashKey
     * @param $value
     * @return bool|int
     */
    public static function hDel($key,$hashKey1, $hashKey2 = null, $hashKeyN = null){
        return self::$handler->hDel($key, $hashKey1, $hashKey2, $hashKeyN);
    }

    /**
     * 判断指定哈希键是否存在
     * @param $key
     * @param $hashKey
     * @return bool
     */
    public static function hExists($key, $hashKey){
        return self::$handler->hExists($key, $hashKey);
    }

    public static function lLen($key){
        return self::$handler->lLen($key);
    }

    public static function lPush($key, $value){
        return self::$handler->lPush($key, $value);
    }

    public static function lPop($key){
        return self::$handler->lPop($key);
    }

    public static function rPush($key, $value){
        return self::$handler->rPush($key, $value);
    }

    public static function rPop($key){
        return self::$handler->rPop($key);
    }

    public static function sAdd($key, $value){
        return self::$handler->sAdd($key, $value);
    }

    public static function sGetMembers($key){
        return self::$handler->sGetMembers($key);
    }

    public static function sRem($key, $value){
        return self::$handler->sRem($key, $value);
    }
}